#include <Windows.h>
#include <winioctl.h>

#include <stdio.h>

#include "log.h"

#define DEVICE_NAME "\\\\.\\TimerDevice"

#define IOCTL_TIMER_START CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_TIMER_STOP CTL_CODE(FILE_DEVICE_UNKNOWN, 0x801, METHOD_BUFFERED, FILE_ANY_ACCESS)

int main()
{
    HANDLE hDevice = INVALID_HANDLE_VALUE;
    ULONG dwLength = 0;

    hDevice = CreateFile(
        DEVICE_NAME,
        GENERIC_READ | GENERIC_WRITE,
        0,
        NULL,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        NULL    
    );
    if (INVALID_HANDLE_VALUE == hDevice)
    {
        Err("[Error] CreateFile");
        return 0;
    }

    do
    {
        if (!DeviceIoControl(
            hDevice,
            IOCTL_TIMER_START,
            NULL, 0,
            NULL, 0,
            &dwLength,
            NULL
        ))
        {
            Err("[Error] CreateFile DeviceIoControl IOCTL_TIMER_START");
            break;
        }

        getchar();

        if (!DeviceIoControl(
            hDevice,
            IOCTL_TIMER_STOP,
            NULL, 0,
            NULL, 0,
            &dwLength,
            NULL
        ))
        {
            Err("[Error] CreateFile DeviceIoControl IOCTL_TIMER_STOP");
            break;
        }
    } while (FALSE);

    CloseHandle(hDevice);
    hDevice = INVALID_HANDLE_VALUE;
    
    return 0;
}